package cn.liumouren.jzxd.controller;

import cn.hutool.extra.pinyin.PinyinUtil;
import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.R;
import cn.liumouren.jzxd.entity.Scene;
import cn.liumouren.jzxd.service.SceneService;
import cn.liumouren.jzxd.utils.QueryUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @author Freeman Liu
 * @email llw599502537@gmail.com
 * @date 2020-10-31 00:44:46
 */
@RestController
@RequestMapping("/scene")
@Api(tags = "景点")
public class SceneController {
    @Autowired
    private SceneService sceneService;

    /**
     * 列表
     */
    @ApiOperation("分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sceneService.queryPage(params);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id查询")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public R info(@PathVariable("id") String id) {
        Scene scene = sceneService.getById(id);

        return R.ok().put("data", scene);
    }

    /**
     * 保存
     */
    @ApiOperation("新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@RequestBody Scene scene) {
        sceneService.save(scene);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public R update(@RequestBody Scene scene) {
        sceneService.updateById(scene);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("批量删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestBody String[] ids) {
        sceneService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation("根据选中tag进行分页查询")
    @RequestMapping(value = "/getSceneByTagIds/{cur}/{size}", method = RequestMethod.POST)
    public R orderByColumns(@PathVariable int cur, @PathVariable int size, @RequestBody List<String> tagIds) {
        IPage<Scene> scenePage = sceneService.getSceneByTagIds(tagIds, cur, size);

        return R.ok("成功查询标签" + tagIds + "下的第[" + cur + "]页(每页显示" + size + ")景点", scenePage);
    }

    @ApiOperation("根据指定 column 分页升降序排序(0升序,1降序) [{'add_time':1},{'hot':0}]")
    @RequestMapping(value = "/pageQueryByColumn/{cur}/{size}", method = RequestMethod.POST)
    public R pageQueryByColumn(@RequestBody Map<String, Integer> map, @PathVariable int cur, @PathVariable int size) {
        // [{'add_time':1},{'hot':0}]
        IPage<Scene> scenes = sceneService.pageQueryByColumn(map, cur, size);
        return R.ok("成功查询第[" + cur + "]页(每页显示[" + size + "])景点", scenes);
    }

    @ApiOperation("返回所有景点名称")
    @RequestMapping(value = "/getSceneNames", method = RequestMethod.GET)
    public R getSceneNames(Integer cur, Integer size) {
        if (cur == null || size == null) {
            // 查询所有
            List<Scene> scenes = sceneService.listIdAndName();
            HashMap<Character, List<Scene>> map = orderByFirstLetter(scenes);
            return R.ok("成功查询所有景点名称", map);
        }
        List<Scene> scenes = sceneService.listPageIdAndName(cur, size);
        HashMap<Character, List<Scene>> m = orderByFirstLetter(scenes);
        return R.ok("成功查询第[" + cur + "]页(每页显示[" + size + "])景点名称", m);
    }

    private HashMap<Character, List<Scene>> orderByFirstLetter(List<Scene> scenes) {
        HashMap<Character, List<Scene>> map = new HashMap<>(32);
        for (char i = 'A'; i <= 'Z'; i++) {
            ArrayList<Scene> list = new ArrayList<>();
            for (Scene scene : scenes) {
                if (StringUtils.equalsIgnoreCase(String.valueOf(PinyinUtil.getFirstLetter(scene.getName().charAt(0))), String.valueOf(i))) {
                    list.add(scene);
                }
            }
            map.put(i, list);
        }
        return map;
    }

    @ApiOperation("根据关键字分页搜索")
    @RequestMapping(value = "/search/{cur}/{size}", method = RequestMethod.GET)
    public R getSceneNames(String key, @PathVariable int cur, @PathVariable int size) {
        IPage<Scene> scenes = sceneService.queryPageByKey(key, cur, size);
        return R.ok("成功根据关键字[" + key + "]查询第[" + cur + "]页(每页显示[" + size + "])景点", scenes);
    }

    @ApiOperation("万能查询")
    @RequestMapping(value = "/customQuery", method = RequestMethod.GET)
    public R getSceneNames(@RequestParam Map<String, Object> map) {
        PageUtils result = QueryUtil.getResult(sceneService, map);
        return R.ok("查询成功", result);
    }

}
